using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using HabilisDesign.Iota.Properties;

namespace HabilisDesign.Iota
{
	/// <summary>
	/// Interaction logic for SettingsWindow.xaml
	/// </summary>

	public partial class SettingsWindow : System.Windows.Window
	{
		#region Fields

		private bool m_HasChanges = false;

		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="SettingsWindow"/> class.
		/// </summary>
		/// <param name="manager">The manager.</param>
		public SettingsWindow()
		{
			InitializeComponent();

			txtUsername.Text = Settings.Default.Username;
			txtPassword.Password = Settings.Default.Password;
			PollingPeriod.Value = Settings.Default.PollingPeriod;
		}

		#endregion


		#region Methods

		#region Dirty Flags

		/// <summary>
		/// Called when [text changed].
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.Controls.TextChangedEventArgs"/> instance containing the event data.</param>
		private void OnTextChanged( object sender, TextChangedEventArgs e )
		{
			m_HasChanges = true;
		}

		/// <summary>
		/// Called when [value changed].
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void OnValueChanged( object sender, RoutedEventArgs e )
		{
			m_HasChanges = true;
		}

		#endregion

		/// <summary>
		/// Begins the move.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
		private void BeginMove( object sender, MouseButtonEventArgs e )
		{
			this.DragMove();
		}

		/// <summary>
		/// Updates the settings.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void UpdateSettings( object sender, RoutedEventArgs e )
		{
			Settings.Default.Username = txtUsername.Text.Trim();
			Settings.Default.Password = txtPassword.Password.Trim();
			Settings.Default.PollingPeriod = PollingPeriod.Value;
			Settings.Default.Save();
			this.DialogResult = m_HasChanges;
			this.Close();
		}

		#endregion



	} // end class

} // end namespace